<template>
  <div class="msgset">
    <MsgTable
      :tableData="maintainList"
      title="维修消息提醒"
      :buttonCodes="buttonCodes"
    ></MsgTable>
    <MsgTable
      :tableData="warrantyList"
      title="维保消息提醒"
      :buttonCodes="buttonCodes"
    ></MsgTable>
    <MsgTable
      :tableData="checkList"
      title="点检消息提醒"
      :buttonCodes="buttonCodes"
    ></MsgTable>
    <MsgTable
      :tableData="partList"
      title="备件消息提醒"
      :buttonCodes="buttonCodes"
    ></MsgTable>
    <MsgTable
      :tableData="transmitList"
      title="转交消息提醒"
      :buttonCodes="buttonCodes"
    ></MsgTable>
  </div>
</template>

<script setup lang="ts">
import { ref, defineAsyncComponent, onBeforeUnmount, onMounted } from 'vue'
import emitter from '/@/utils/mitt'
import { getMsgTemplateList } from '/@/api/setting/enterpriseInfo'
import { getButtonListByRoleApi } from '/@/api/publicApi'
import { Session } from '/@/utils/storage'
const menu = Session.get('menu')
const buttonCodes = ref<any>([])
const MsgTable = defineAsyncComponent(
  () => import('/@/views/setting/components/MsgTable.vue')
)

let [maintainList, warrantyList, checkList, partList, transmitList] = [
  ref(<any>[]),
  ref(<any>[]),
  ref(<any>[]),
  ref(<any>[]),
  ref(<any>[])
]
const getTemplate = async () => {
  maintainList.value = []
  warrantyList.value = []
  checkList.value = []
  partList.value = []
  transmitList.value = []
  let res = await getMsgTemplateList()
  if (res.code == 0) {
    res.data.forEach((element: any) => {
      if ([1, 2, 3, 4].indexOf(element.msgType) != -1) {
        maintainList.value.push(element)
      } else if ([5, 6].indexOf(element.msgType) != -1) {
        warrantyList.value.push(element)
      } else if ([7, 8].indexOf(element.msgType) != -1) {
        checkList.value.push(element)
      } else if ([9, 10, 11, 12, 13, 14].indexOf(element.msgType) != -1) {
        partList.value.push(element)
      } else {
        transmitList.value.push(element)
      }
    })
  }
}
getTemplate()

emitter.on('msgFlash', (node) => {
  getTemplate()
})

onBeforeUnmount(() => {
  emitter.off('msgFlash')
})

onMounted(() => {
  menu.forEach(async (element: any) => {
    if (element.menuUrl == '/setting/EnterpriseInfo') {
      let res = await getButtonListByRoleApi({
        terminalType: 1,
        menuPid: element.id
      })
      res.data.forEach((item: any) => {
        buttonCodes.value.push(item.buttonCode)
      })
    }
  })
})
</script>

<style scoped lang="scss"></style>
